//
//  FriendsterHeaderViewController.m
//  BaChat
//
//  Created by dllo on 16/6/16.
//  Copyright © 2016年 刘洋. All rights reserved.
//

#import "FriendsterHeaderViewController.h"
#import "DiscoveryTableViewCell.h"

static NSString *const HeaderCell = @"HeaderCell";
@interface FriendsterHeaderViewController ()<UITableViewDataSource,UITableViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (nonatomic, retain)BaseTableView *HeaderTableView;
@property (nonatomic, retain)NSArray *array;

@end

@implementation FriendsterHeaderViewController
-(void)dealloc
{
    [_HeaderTableView release];
    [_array release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor grayColor];
    
     self.array = @[@"从手机相册选择",@"拍一张"];
    self.navigationItem.title = @"更换相册封面";
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.HeaderTableView = [[BaseTableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.HeaderTableView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:self.HeaderTableView];
    [self.HeaderTableView release];
    self.HeaderTableView.delegate = self;
    self.HeaderTableView.dataSource = self;
    
    self.HeaderTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    //注册
    [self.HeaderTableView registerClass:[DiscoveryTableViewCell class] forCellReuseIdentifier:HeaderCell];
    
    
    
}



-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DiscoveryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:HeaderCell forIndexPath:indexPath];
    
   
    cell.label.text = _array[indexPath.row];
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        //获取系统相册
        UIImagePickerController *controll = [[UIImagePickerController alloc]init];
        controll.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        //允许编辑
        controll.allowsEditing = YES;
        controll.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
        //选择样式
//        controll.navigationBar.barStyle = UIBarStyleBlack;
//    /    这是导航栏颜色
//        [controll.navigationBar setTintColor:[UIColor whiteColor]];
        
        controll.delegate = self;
        //跳转到系统相册
        [self presentViewController:controll animated:YES completion:^{
            
            
        }];
        [controll release];

    }
    if (indexPath.row == 1) {
        //获取拍摄照片
        UIImagePickerController *imagePC = [[UIImagePickerController alloc]init];
        imagePC.sourceType = UIImagePickerControllerSourceTypeCamera;
        imagePC.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
        //允许编辑
        imagePC.allowsEditing = YES;
        //选择样式
//        imagePC.navigationBar.barStyle = UIBarStyleBlack;
        //这是导航栏颜色
//        [imagePC.navigationBar setTintColor:[UIColor whiteColor]];
        
        imagePC.delegate = self;
        //跳转到系统相册
        [self presentViewController:imagePC animated:YES completion:^{
            
            
        }];
        [imagePC release];
    }
}

//回调
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    UIImage *image = [info valueForKey:@"UIImagePickerControllerEditedImage"];
    
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeImage" object:image];
    
       //关闭相册界面
    [picker dismissViewControllerAnimated:YES completion:^{
        
        
    }];
    
}

@end
